﻿using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using Easy.Common;

namespace Easy.DAL
{
    public class Xmlload
    {
        public bool DataToXml(string entityname, Guid formid)
        {
            SqlDatabase sqldb = new SqlDatabase(Config.TulingBuyCrmConnectString);
            // 创建dom对象 // 创建根节点root 
            XmlDocument doc = new XmlDocument();
            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", ""));
            XmlElement root = doc.CreateElement("root");
            doc.AppendChild(root);

            //获取该条记录对应的中间表
            string SqlGetRelationMap = @"SELECT * FROM RelationMap where EntityName1=@EntityName1";
            DbCommand SqlGetRelationMapcmd = sqldb.GetSqlStringCommand(SqlGetRelationMap);
            SqlGetRelationMapcmd.Parameters.Add(new SqlParameter("@EntityName1", entityname));
            DataSet SqlGetRelationMapds = sqldb.ExecuteDataSet(SqlGetRelationMapcmd);
            DataTable SqlGetRelationMapdt = SqlGetRelationMapds.Tables[0];

            for (int i = 0; i < SqlGetRelationMapdt.Rows.Count; i++)
            {
                string EntityName2 = SqlGetRelationMapdt.Rows[i]["EntityName2"].ToString();
                string RelationName = SqlGetRelationMapdt.Rows[i]["RelationName"].ToString();
                XmlElement part = doc.CreateElement(EntityName2);
                root.AppendChild(part);    //  加入到xml document
                //获取该中间表新建的记录
                string SqlGetEntityName2id = @"SELECT " + EntityName2 + "Id FROM  " + RelationName + "  where SolutionId=@SolutionId";
                DbCommand SqlGetEntityName2cmd = sqldb.GetSqlStringCommand(SqlGetEntityName2id);
                SqlGetEntityName2cmd.Parameters.Add(new SqlParameter("@SolutionId", formid.ToString()));
                DataSet SqlGetEntityName2ds = sqldb.ExecuteDataSet(SqlGetEntityName2cmd);
                DataTable SqlGetEntityName2dt = SqlGetEntityName2ds.Tables[0];
                if (SqlGetEntityName2dt.Rows.Count == 0)
                {
                    XmlElement record = doc.CreateElement("record"); 
                    record.InnerText = "无记录";
                    part.AppendChild(record);   // 添加到part
                    continue;
                }
                else
                {
                    //获取新建的记录保存到xml文件中
                    for (int j = 0; j < SqlGetEntityName2dt.Rows.Count; j++)
                    {
                        string SqlGetEntityName2d = @"SELECT * FROM  " + EntityName2 + " where  " + EntityName2 + "Id =@EntityName2Id";
                        DbCommand SqlGetEntityName2dcmd = sqldb.GetSqlStringCommand(SqlGetEntityName2d);
                        SqlGetEntityName2dcmd.Parameters.Add(new SqlParameter("@EntityName2Id", SqlGetEntityName2dt.Rows[j][0]));
                        DataSet SqlGetEntityName2dds = sqldb.ExecuteDataSet(SqlGetEntityName2dcmd);
                        DataTable SqlGetEntityName2ddt = SqlGetEntityName2dds.Tables[0];
                        XmlElement record = doc.CreateElement("record");  // 创建record元素     
                        part.AppendChild(record);   // 添加到part
                        for (int k = 0; k < SqlGetEntityName2ddt.Columns.Count; k++)
                        {
                            for (int z = 0; z < SqlGetEntityName2ddt.Rows.Count; z++)
                            {
                                XmlElement attr = doc.CreateElement(SqlGetEntityName2ddt.Columns[k].ColumnName.ToString());
                                attr.InnerText = (SqlGetEntityName2ddt.Rows[z][k]).ToString();
                                record.AppendChild(attr);
                            }

                        }
                        if (EntityName2 == "RoleMap")
                        {
                            string roleid = (SqlGetEntityName2ddt.Rows[0][0]).ToString();
                            //角色关联的实体权限关系
                            string SqlGetrolepr = @"SELECT * FROM  RolePrivilageRelationship  where  RoleId =@RoleId";
                            DbCommand SqlGetroleprcmd = sqldb.GetSqlStringCommand(SqlGetrolepr);
                            SqlGetroleprcmd.Parameters.Add(new SqlParameter("@RoleId", roleid));
                            DataSet SqlGetroleprds = sqldb.ExecuteDataSet(SqlGetroleprcmd);
                            DataTable SqlGetroleprdt = SqlGetroleprds.Tables[0];
                            XmlElement rolepr = doc.CreateElement("RolePrivilageRelationship");
                            XmlElement pr = doc.CreateElement("Privialge");
                            if (SqlGetroleprdt.Rows.Count == 0)
                            {
                                XmlElement recordvj = doc.CreateElement("record");
                                rolepr.AppendChild(recordvj);
                                pr.AppendChild(recordvj);
                                recordvj.InnerText = "无记录";
                            }
                            else
                            {
                                for (int rz = 0; rz < SqlGetroleprdt.Rows.Count; rz++)
                                {
                                    XmlElement recordpr = doc.CreateElement("record");
                                    rolepr.AppendChild(recordpr);
                                    for (int rk = 0; rk < SqlGetroleprdt.Columns.Count; rk++)
                                    {
                                        XmlElement attr = doc.CreateElement(SqlGetroleprdt.Columns[rk].ColumnName.ToString());
                                        attr.InnerText = (SqlGetroleprdt.Rows[rz][rk]).ToString();
                                        recordpr.AppendChild(attr);

                                        //角色关联实体权限的权限表
                                        if (SqlGetroleprdt.Columns[rk].ColumnName.ToString() == "PrivilageId")
                                        {
                                            string SqlGetvj = @"select * from Privialge as p where
                                                            p.PrivilageId=@PrivilageId";
                                            DbCommand SqlGetvjcmd = sqldb.GetSqlStringCommand(SqlGetvj);
                                            SqlGetvjcmd.Parameters.Add(new SqlParameter("@PrivilageId", SqlGetroleprdt.Rows[rz][rk].ToString()));
                                            DataSet SqlGetvjds = sqldb.ExecuteDataSet(SqlGetvjcmd);
                                            DataTable SqlGetvjdt = SqlGetvjds.Tables[0];
                                            if (SqlGetvjdt.Rows.Count == 0)
                                            {
                                                XmlElement recordvj = doc.CreateElement("record");
                                                pr.AppendChild(recordvj);
                                                recordvj.InnerText = "无记录";
                                            }
                                            else
                                            {
                                                for (int ri = 0; ri < SqlGetvjdt.Rows.Count; ri++)
                                                {
                                                    XmlElement recordvj = doc.CreateElement("record");
                                                    pr.AppendChild(recordvj);
                                                    for (int rj = 0; rj < SqlGetvjdt.Columns.Count; rj++)
                                                    {
                                                        XmlElement attri = doc.CreateElement(SqlGetvjdt.Columns[rj].ColumnName.ToString());
                                                        attri.InnerText = (SqlGetvjdt.Rows[ri][rj]).ToString();
                                                        recordvj.AppendChild(attri);
                                                    }
                                                }
                                            }
                                        }

                                    }
                                }
                            }
                            root.AppendChild(rolepr);
                            root.AppendChild(pr);
                            //角色关联的站点图配置关系
                            string SqlGetrolest = @"SELECT * FROM  SiteMapRoleMapRelationship  where  RoleMapId =@RoleMapId";
                            DbCommand SqlGetrolestcmd = sqldb.GetSqlStringCommand(SqlGetrolest);
                            SqlGetrolestcmd.Parameters.Add(new SqlParameter("@RoleMapId", roleid));
                            DataSet SqlGetrolestds = sqldb.ExecuteDataSet(SqlGetrolestcmd);
                            DataTable SqlGetrolestdt = SqlGetrolestds.Tables[0];
                            XmlElement rolest = doc.CreateElement("SiteMapRoleMapRelationship");
                            if (SqlGetrolestdt.Rows.Count == 0)
                            {
                                XmlElement recordvj = doc.CreateElement("record");
                                rolest.AppendChild(recordvj);
                                recordvj.InnerText = "无记录";
                            }
                            else
                            {
                                for (int rz = 0; rz < SqlGetrolestdt.Rows.Count; rz++)
                                {
                                    XmlElement recordst = doc.CreateElement("record");
                                    rolest.AppendChild(recordst);
                                    for (int rk = 0; rk < SqlGetrolestdt.Columns.Count; rk++)
                                    {
                                        XmlElement attr = doc.CreateElement(SqlGetrolestdt.Columns[rk].ColumnName.ToString());
                                        attr.InnerText = (SqlGetrolestdt.Rows[rz][rk]).ToString();
                                        recordst.AppendChild(attr);
                                    }
                                }
                            }
                            root.AppendChild(rolest);
                            //角色关联的用户关系
                            string SqlGetroleuser = @"SELECT * FROM  UserRoleRelationship  where  RoleMapId =@RoleMapId";
                            DbCommand SqlGetroleusercmd = sqldb.GetSqlStringCommand(SqlGetroleuser);
                            SqlGetroleusercmd.Parameters.Add(new SqlParameter("@RoleMapId", roleid));
                            DataSet SqlGetroleuserds = sqldb.ExecuteDataSet(SqlGetroleusercmd);
                            DataTable SqlGetroleuserdt = SqlGetroleuserds.Tables[0];
                            XmlElement roleuser = doc.CreateElement("UserRoleRelationship");
                            XmlElement ssu = doc.CreateElement("SystemUser");
                            if (SqlGetroleuserdt.Rows.Count == 0)
                            {
                                XmlElement recordvj = doc.CreateElement("record");
                                roleuser.AppendChild(recordvj);
                                ssu.AppendChild(recordvj);
                                recordvj.InnerText = "无记录";
                            }
                            else
                            {
                                for (int rz = 0; rz < SqlGetroleuserdt.Rows.Count; rz++)
                                {
                                    XmlElement recordur = doc.CreateElement("record");
                                    roleuser.AppendChild(recordur);
                                    for (int rk = 0; rk < SqlGetroleuserdt.Columns.Count; rk++)
                                    {
                                        XmlElement attr = doc.CreateElement(SqlGetroleuserdt.Columns[rk].ColumnName.ToString());
                                        attr.InnerText = (SqlGetroleuserdt.Rows[rz][rk]).ToString();
                                        recordur.AppendChild(attr);


                                        //角色关联用户的用户表
                                        if (SqlGetroleuserdt.Columns[rk].ColumnName.ToString() == "SystemUserId")
                                        {

                                            string SqlGetvj = @"select * from SystemUser as s where
                                                            s.SystemUserId=@SystemUserId";
                                            DbCommand SqlGetvjcmd = sqldb.GetSqlStringCommand(SqlGetvj);
                                            SqlGetvjcmd.Parameters.Add(new SqlParameter("@SystemUserId", SqlGetroleuserdt.Rows[rz][rk].ToString()));
                                            DataSet SqlGetvjds = sqldb.ExecuteDataSet(SqlGetvjcmd);
                                            DataTable SqlGetvjdt = SqlGetvjds.Tables[0];
                                            for (int ri = 0; ri < SqlGetvjdt.Rows.Count; ri++)
                                            {
                                                XmlElement recordvj = doc.CreateElement("record");
                                                ssu.AppendChild(recordvj);
                                                for (int rj = 0; rj < SqlGetvjdt.Columns.Count; rj++)
                                                {
                                                    XmlElement attri = doc.CreateElement(SqlGetvjdt.Columns[rj].ColumnName.ToString());
                                                    attri.InnerText = (SqlGetvjdt.Rows[ri][rj]).ToString();
                                                    recordvj.AppendChild(attri);
                                                }
                                            }

                                        }

                                    }
                                }
                            }
                            root.AppendChild(roleuser);
                            root.AppendChild(ssu);
                            //角色关联的功能权限关系
                            string SqlGetrolefc = @"SELECT * FROM  RoleMapFunctionPrivilegeRelationship  where  RoleMapId =@RoleMapId";
                            DbCommand SqlGetrolefccmd = sqldb.GetSqlStringCommand(SqlGetrolefc);
                            SqlGetrolefccmd.Parameters.Add(new SqlParameter("@RoleMapId", roleid));
                            DataSet SqlGetrolefcds = sqldb.ExecuteDataSet(SqlGetrolefccmd);
                            DataTable SqlGetrolefcdt = SqlGetrolefcds.Tables[0];
                            XmlElement rolefc = doc.CreateElement("RoleMapFunctionPrivilegeRelationship");
                            if (SqlGetrolefcdt.Rows.Count == 0)
                            {
                                XmlElement recordvj = doc.CreateElement("record");
                                rolefc.AppendChild(recordvj);
                                recordvj.InnerText = "无记录";
                            }
                            else
                            {
                                for (int rz = 0; rz < SqlGetrolefcdt.Rows.Count; rz++)
                                {
                                    XmlElement recordfc = doc.CreateElement("record");
                                    rolefc.AppendChild(recordfc);
                                    for (int rk = 0; rk < SqlGetrolefcdt.Columns.Count; rk++)
                                    {
                                        XmlElement attr = doc.CreateElement(SqlGetrolefcdt.Columns[rk].ColumnName.ToString());
                                        attr.InnerText = (SqlGetrolefcdt.Rows[rz][rk]).ToString();
                                        recordfc.AppendChild(attr);
                                    }
                                }
                            }
                            root.AppendChild(rolefc);
                        }
                        if (EntityName2 == "EntityMap")
                        {
                            string EntityId = (SqlGetEntityName2ddt.Rows[0]["EntityMapId"]).ToString();
                            string EntityName = (SqlGetEntityName2ddt.Rows[0]["EntityName"]).ToString();

                            //实体关联的多对多关系
                            string SqlGetetnn2n = @"SELECT * FROM  RelationMap  where  EntityName1 =@EntityName";
                            DbCommand SqlGetetnn2ncmd = sqldb.GetSqlStringCommand(SqlGetetnn2n);
                            SqlGetetnn2ncmd.Parameters.Add(new SqlParameter("@EntityName", EntityName));
                            DataSet SqlGetetnn2nds = sqldb.ExecuteDataSet(SqlGetetnn2ncmd);
                            DataTable SqlGetetnn2ndt = SqlGetetnn2nds.Tables[0];
                            XmlElement etnn2n = doc.CreateElement("RelationMap");
                            if (SqlGetetnn2ndt.Rows.Count == 0)
                            {
                                XmlElement recordvj = doc.CreateElement("record");
                                etnn2n.AppendChild(recordvj);
                                recordvj.InnerText = "无记录";
                            }
                            else
                            {
                                for (int rz = 0; rz < SqlGetetnn2ndt.Rows.Count; rz++)
                                {
                                    XmlElement recordetnn2n = doc.CreateElement("record");
                                    etnn2n.AppendChild(recordetnn2n);
                                    for (int rk = 0; rk < SqlGetetnn2ndt.Columns.Count; rk++)
                                    {
                                        XmlElement attr = doc.CreateElement(SqlGetetnn2ndt.Columns[rk].ColumnName.ToString());
                                        attr.InnerText = (SqlGetetnn2ndt.Rows[rz][rk]).ToString();
                                        recordetnn2n.AppendChild(attr);
                                    }
                                }
                            }
                            root.AppendChild(etnn2n);

                            //实体关联的字段
                            string SqlGetetnattr = @"SELECT * FROM  AttributeMap  where  EntityId =@EntityId";
                            DbCommand SqlGetetnattrcmd = sqldb.GetSqlStringCommand(SqlGetetnattr);
                            SqlGetetnattrcmd.Parameters.Add(new SqlParameter("@EntityId", EntityId));
                            DataSet SqlGetetnattrds = sqldb.ExecuteDataSet(SqlGetetnattrcmd);
                            DataTable SqlGetetnattrdt = SqlGetetnattrds.Tables[0];
                            XmlElement etnattr = doc.CreateElement("ViewAttMap");
                            if (SqlGetetnattrdt.Rows.Count == 0)
                            {
                                XmlElement recordvj = doc.CreateElement("record");
                                etnattr.AppendChild(recordvj);
                                recordvj.InnerText = "无记录";
                            }
                            else
                            {
                                for (int rz = 0; rz < SqlGetetnattrdt.Rows.Count; rz++)
                                {
                                    XmlElement recordetnattr = doc.CreateElement("record");
                                    etnattr.AppendChild(recordetnattr);
                                    for (int rk = 0; rk < SqlGetetnattrdt.Columns.Count; rk++)
                                    {
                                        XmlElement attr = doc.CreateElement(SqlGetetnattrdt.Columns[rk].ColumnName.ToString());
                                        attr.InnerText = (SqlGetetnattrdt.Rows[rz][rk]).ToString();
                                        recordetnattr.AppendChild(attr);
                                    }
                                }
                            }
                            root.AppendChild(etnattr);

                            //实体关联的view视图
                            string SqlGetetnview = @"SELECT * FROM  ViewMap as v where
                                                      v.EntityName =@EntityName and v.DeletionStateCode=0";
                            DbCommand SqlGetetnviewcmd = sqldb.GetSqlStringCommand(SqlGetetnview);
                            SqlGetetnviewcmd.Parameters.Add(new SqlParameter("@EntityName", EntityName));
                            DataSet SqlGetetnviewds = sqldb.ExecuteDataSet(SqlGetetnviewcmd);
                            DataTable SqlGetetnviewdt = SqlGetetnviewds.Tables[0];
                            XmlElement etnview = doc.CreateElement("ViewMap");
                            XmlElement vj = doc.CreateElement("ViewMapJsListRelationship");
                            if (SqlGetetnviewdt.Rows.Count == 0)
                            {
                                XmlElement recordvj = doc.CreateElement("record");
                                etnview.AppendChild(recordvj);
                                vj.AppendChild(recordvj);
                                recordvj.InnerText = "无记录";
                            }
                            else
                            {
                                for (int rz = 0; rz < SqlGetetnviewdt.Rows.Count; rz++)
                                {
                                    XmlElement recordetnview = doc.CreateElement("record");
                                    etnview.AppendChild(recordetnview);
                                    for (int rk = 0; rk < SqlGetetnviewdt.Columns.Count; rk++)
                                    {
                                        XmlElement attr = doc.CreateElement(SqlGetetnviewdt.Columns[rk].ColumnName.ToString());
                                        attr.InnerText = (SqlGetetnviewdt.Rows[rz][rk]).ToString();
                                        recordetnview.AppendChild(attr);

                                        //实体关联view的js关联关系表

                                        if (rk == 0)
                                        {
                                            string SqlGetvj = @"select * from ViewMapJsListRelationship as vj where
                                                            vj.ViewMapId=@ViewMapId";
                                            DbCommand SqlGetvjcmd = sqldb.GetSqlStringCommand(SqlGetvj);
                                            SqlGetvjcmd.Parameters.Add(new SqlParameter("@ViewMapId", SqlGetetnviewdt.Rows[rz][rk].ToString()));
                                            DataSet SqlGetvjds = sqldb.ExecuteDataSet(SqlGetvjcmd);
                                            DataTable SqlGetvjdt = SqlGetvjds.Tables[0];
                                            if (SqlGetvjdt.Rows.Count == 0)
                                            {
                                                XmlElement recordvj = doc.CreateElement("record");
                                                vj.AppendChild(recordvj);
                                                recordvj.InnerText = "无记录";
                                            }
                                            else
                                            {
                                                for (int ri = 0; ri < SqlGetvjdt.Rows.Count; ri++)
                                                {
                                                    XmlElement recordvj = doc.CreateElement("record");
                                                    vj.AppendChild(recordvj);
                                                    for (int rj = 0; rj < SqlGetvjdt.Columns.Count; rj++)
                                                    {
                                                        XmlElement attri = doc.CreateElement(SqlGetvjdt.Columns[rj].ColumnName.ToString());
                                                        attri.InnerText = (SqlGetvjdt.Rows[ri][rj]).ToString();
                                                        recordvj.AppendChild(attri);
                                                    }
                                                }
                                            }

                                        }
                                    }
                                }
                            }
                            root.AppendChild(etnview);
                            root.AppendChild(vj);
                            //实体关联的form视图
                            string SqlGetetnform = @"select * from Form as f where
                                                     f.EntityName=@EntityName and f.DeletionStateCode=0";
                            DbCommand SqlGetetnformcmd = sqldb.GetSqlStringCommand(SqlGetetnform);
                            SqlGetetnformcmd.Parameters.Add(new SqlParameter("@EntityName", EntityName));
                            DataSet SqlGetetnformds = sqldb.ExecuteDataSet(SqlGetetnformcmd);
                            DataTable SqlGetetnformdt = SqlGetetnformds.Tables[0];
                            XmlElement etnform = doc.CreateElement("Form");
                            XmlElement fj = doc.CreateElement("FormJsListRelationship");
                            if (SqlGetetnformdt.Rows.Count == 0)
                            {
                                XmlElement recordvj = doc.CreateElement("record");
                                etnform.AppendChild(recordvj);
                                fj.AppendChild(recordvj);
                                recordvj.InnerText = "无记录";
                            }
                            else
                            {
                                for (int rz = 0; rz < SqlGetetnformdt.Rows.Count; rz++)
                                {
                                    XmlElement recordetnform = doc.CreateElement("record");
                                    etnform.AppendChild(recordetnform);
                                    for (int rk = 0; rk < SqlGetetnformdt.Columns.Count; rk++)
                                    {
                                        XmlElement attr = doc.CreateElement(SqlGetetnformdt.Columns[rk].ColumnName.ToString());
                                        attr.InnerText = (SqlGetetnformdt.Rows[rz][rk]).ToString();
                                        recordetnform.AppendChild(attr);
                                        //实体关联form的js关联关系表
                                        if (rk == 0)
                                        {

                                            string SqlGetfj = @"select * from FormJsListRelationship as fj where
                                                            fj.FormId=@FormId";
                                            DbCommand SqlGetfjcmd = sqldb.GetSqlStringCommand(SqlGetfj);
                                            SqlGetfjcmd.Parameters.Add(new SqlParameter("@FormId", SqlGetetnformdt.Rows[rz][rk].ToString()));
                                            DataSet SqlGetfjds = sqldb.ExecuteDataSet(SqlGetfjcmd);
                                            DataTable SqlGetfjdt = SqlGetfjds.Tables[0];
                                            if (SqlGetfjdt.Rows.Count == 0)
                                            {
                                                XmlElement recordvj = doc.CreateElement("record");
                                                fj.AppendChild(recordvj);
                                                recordvj.InnerText = "无记录";
                                            }
                                            else
                                            {
                                                for (int ri = 0; ri < SqlGetfjdt.Rows.Count; ri++)
                                                {
                                                    XmlElement recordfj = doc.CreateElement("record");
                                                    fj.AppendChild(recordfj);
                                                    for (int rj = 0; rj < SqlGetfjdt.Columns.Count; rj++)
                                                    {
                                                        XmlElement attri = doc.CreateElement(SqlGetfjdt.Columns[rj].ColumnName.ToString());
                                                        attri.InnerText = (SqlGetfjdt.Rows[ri][rj]).ToString();
                                                        recordfj.AppendChild(attri);
                                                    }
                                                }
                                            }
                                        }
                                    }

                                }
                            }
                            root.AppendChild(etnform);
                            root.AppendChild(fj);
                            //实体关联的关联列表
                            string SqlGetetnlk = @"SELECT * FROM  EntityLink  where  Entity =@EntityName";
                            DbCommand SqlGetetnlkcmd = sqldb.GetSqlStringCommand(SqlGetetnlk);
                            SqlGetetnlkcmd.Parameters.Add(new SqlParameter("@EntityName", EntityName));
                            DataSet SqlGetetnlkds = sqldb.ExecuteDataSet(SqlGetetnlkcmd);
                            DataTable SqlGetetnlkdt = SqlGetetnlkds.Tables[0];
                            XmlElement etnlk = doc.CreateElement("EntityLink");
                            if (SqlGetetnlkdt.Rows.Count == 0)
                            {
                                XmlElement recordvj = doc.CreateElement("record");
                                etnlk.AppendChild(recordvj);
                                recordvj.InnerText = "无记录";
                            }
                            else
                            {
                                for (int rz = 0; rz < SqlGetetnlkdt.Rows.Count; rz++)
                                {
                                    XmlElement recordetnlk = doc.CreateElement("record");
                                    etnlk.AppendChild(recordetnlk);
                                    for (int rk = 0; rk < SqlGetetnlkdt.Columns.Count; rk++)
                                    {
                                        XmlElement attr = doc.CreateElement(SqlGetetnlkdt.Columns[rk].ColumnName.ToString());
                                        attr.InnerText = (SqlGetetnlkdt.Rows[rz][rk]).ToString();
                                        recordetnlk.AppendChild(attr);
                                    }
                                }
                            }
                            root.AppendChild(etnlk);
                        }
                    }
                }

            }
             //下载的文件夹的路径
            string filepath ="C:\\TulingBuyXml\\"+DateTime.Now.ToString("yyyy-MM-dd");
            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);
            }
            string filename = filepath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".xml";
            FileStream fs = File.Create(filename); 
            fs.Close();
             // 保存文件
            doc.Save(filename);   

            return true;
        }
    }
}
